package com.sudao.android.ball;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BestRecord {
	
    private static final int RECORD_COUNT = 10;
	private static final String SCORE = "score";
	private static final String DATE = "date";
	private static final String[] KEY_SCORES = new String[RECORD_COUNT];
	private static final String[] KEY_DATES = new String[RECORD_COUNT];

	public static class Record {
		public String score;
		public String date;

		Record(String s, String d) {
			score = s;
			date = d;
		}
    }

	static {
	    for (int i = 0; i < RECORD_COUNT; i++) {
		    KEY_SCORES[i] = SCORE + i;
		    KEY_DATES[i] = DATE + i;
		}
	}
	
	public static ArrayList<Record> loadRecordReverse(Context context) {
	    ArrayList<Record> records = new ArrayList<Record>();
	    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		for (int i = RECORD_COUNT - 1; i >= 0; i--) {
		    String score = pref.getString(KEY_SCORES[i], null);
			if (score != null) {
		    	String date = pref.getString(KEY_DATES[i], null);
				records.add(new Record(score, date));
			}
		}
		return records;
	}

	public static ArrayList<Record> loadRecord(Context context) {
	    ArrayList<Record> records = new ArrayList<Record>();
	    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		for (int i = 0; i < RECORD_COUNT; i++) {
		    String score = pref.getString(KEY_SCORES[i], null);
			if (score != null) {
		    	String date = pref.getString(KEY_DATES[i], null);
				records.add(new Record(score, date));
			}
		}
		return records;
	}

	private static String getFormatTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
 		Date now = new Date();
 		return formatter.format(now);
	}

	public static void saveRecord(Context context, int d) {
    	ArrayList<Record> records = loadRecord(context);
	    ArrayList<Integer> dataSet = new ArrayList<Integer>();
		for (int i = 0; i < records.size(); i++) {
	    	dataSet.add(new Integer(records.get(i).score));
		}
		boolean inserted = false;
		for (int i = 0; i < dataSet.size(); i++) {
	    	if (d > dataSet.get(i).intValue()) {
				continue;
			} else {
				inserted = true;
		        dataSet.add(i, new Integer(d));
				records.add(i, new Record(d+"", getFormatTime()));
		        break;
			}
		}
		if (!inserted) {
			dataSet.add(new Integer(d));
			records.add(new Record(d+"", getFormatTime()));
		}
    	if (dataSet.size() > RECORD_COUNT) {
	    	dataSet.remove(0); // remove the first object
			records.remove(0);
		}

    	SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
	    for (int i = 0; i < dataSet.size(); i++) {
			editor.putString(KEY_SCORES[i], records.get(i).score);
			editor.putString(KEY_DATES[i], records.get(i).date);
		}
		editor.commit();
	}
}
